package com.subaiwang.controller;

import com.alibaba.fastjson.JSON;
import com.subaiwang.pojo.Pages;
import com.subaiwang.pojo.Picture;
import com.subaiwang.service.picture.PictureService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/picture")
public class PictureController {
    @Resource
    private PictureService pictureService;

    /**
     * 图片管理  --图片列表
     * @return
     */
    @RequestMapping("/advertising.html")
    public String advertising(HttpSession session){
        //设置一个页码
        int pageNo=1;
        Pages pages=new Pages();
        pages.setPageNo(pageNo);
        int count=pictureService.pictureCount(null);
        pages.setPageCount(count);
        pictureService.getPicture(null,pages);

        session.setAttribute("count",count);
        session.setAttribute("list",pages);
        return "advertising";
    }

    /**
     *  图片管理  --图片列表  分页显示
     * @param session
     *
     * @param pageNo
     * @return
     */

    @RequestMapping(value = "/pictureListajax",method = RequestMethod.POST)
    @ResponseBody
    public Object doPost(HttpSession session,
                         @RequestParam(value = "classify",required = false) String classify,
                         @RequestParam(value = "pageNo",required = false,defaultValue = "1") int pageNo){
        int pageNoStr=pageNo;
        int pageNo2=1;
        if (pageNoStr==0) {
            pageNo2=1;
        } else {
            pageNo2=pageNoStr;
        }

        Pages pages = new Pages();
        pages.setPageNo(pageNo2);
        int count=pictureService.pictureCount(classify);
        pages.setPageCount(count);
        pictureService.getPicture(classify,pages);

        session.setAttribute("count",count);
        session.removeAttribute("list");
        session.setAttribute("list",pages);
        String jsonarray = JSON.toJSONString(pages);
        return jsonarray;
    }


}
